#include <memory>
#include "Common.hpp"
#include "TcpServer.hpp"
#include "Protocol_Netcal.hpp"
#include "NetCal.hpp"
#include "Daemon.hpp"

void Usage(std::string cmd){
    std::cout << "please use : " << cmd << " server_port" << std::endl;
    exit(USAGE_ERROR); 
}


// ./Server_Netcal.exe server_port
int main(int argc, char* argv[]){
    if(argc != 2){
        Usage(argv[0]);
    }

    Daemon(0, 0);
    //daemon(0, 0);

    //打印到文件上去！
    ENABLE_FILE_LOG_STRATEGY();


    std::unique_ptr<Calculator> pcal = std::make_unique<Calculator>();

    std::unique_ptr<Protocol_Netcal> protocol = std::make_unique<Protocol_Netcal>
    ([&pcal](int left, int right, char opr)->Response{return pcal->Calculate(left, right, opr);});

    std::unique_ptr<TcpServer> Pserver = std::make_unique<TcpServer>(std::stoi(argv[1]), 
    [&protocol](std::shared_ptr<Socket> socket, const InetAddr& addr){
        protocol->GetRequest(socket, addr);
    });

    Pserver->Init();
    Pserver->Run();

    return 0;
}